<template>
  <div>
    <el-row>
      <el-col :span="24">
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item label="">
            <el-input
              v-model="foodName"
              size="medium"
              placeholder="查询设备名称"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              size="small"
              type="success"
              @click="production"
              icon="el-icon-search"
              >查询</el-button
            >
          </el-form-item>
          <el-form-item label="">
            <el-button
              size="small"
              type="warning"
              @click="notEmpty"
              icon="el-icon-delete-solid"
              >清空</el-button
            >
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              size="small"
              @click="addition(1)"
              icon="el-icon-circle-plus-outline"
              >新增</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="24" style="margin-bottom: 10px"> </el-col>
      <el-col :span="24">
        <div>
          <el-table :data="tableData" border style="width: 100%">
            <el-table-column
              type="index"
              width="70"
              label="序号"
              align="center"
            >
            </el-table-column>
            <el-table-column
              prop="equipmentNumber"
              label="设备编号"
              width=""
              align="center"
            >
            </el-table-column>
            <el-table-column
              prop="equipmentName"
              label="设备名称"
              align="center"
            >
            </el-table-column>
            <el-table-column prop="model" label="型号" align="center">
            </el-table-column>
            <el-table-column prop="startDate" label="启用日期" align="center">
            </el-table-column>
            <el-table-column
              prop="userDepartment"
              label="使用部门"
              align="center"
            >
            </el-table-column>
            <el-table-column prop="management" label="管理人员" align="center">
            </el-table-column>
            <el-table-column label="设备状态" align="center">
              <template #default="scope">
                <el-tag type="success" v-if="scope.row.deviceStatus == 1"
                  >运行</el-tag
                >
                <el-tag type="danger" v-else>停用</el-tag>
              </template>
            </el-table-column>

            <el-table-column label="操作" width="180" align="center">
              <template #default="scope">
                <el-button size="mini" @click="editorPara(scope.row, 2)"
                  >编辑</el-button
                >
                <el-button size="mini" type="danger" @click="deleted(scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <div class="pagingClass">
            <el-pagination
              :page-sizes="m_page.sizes"
              :page-size="m_page.size"
              :current-page="m_page.number"
              @size-change="m_handleSizeChange"
              @current-change="m_handlePageChange"
              layout="total,sizes,prev, pager, next"
              background
              :total="m_page.totalElements"
            ></el-pagination>
          </div>
        </div>
      </el-col>
    </el-row>
    <el-dialog
      :title="single"
      v-model="plassets"
      width="500px"
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="设备编号">
          <el-input v-model="form.number" style="width: 350px"></el-input>
        </el-form-item>
        <el-form-item label="设备名称">
          <el-input v-model="form.offering" style="width: 350px"></el-input>
        </el-form-item>
        <el-form-item label="型号">
          <el-input v-model="form.mock" style="width: 350px"></el-input>
        </el-form-item>
        <el-form-item label="启用日期">
          <el-date-picker
            v-model="form.dates"
            type="date"
            placeholder="选择日期"
            format="YYYY 年 MM 月 DD 日"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="使用部门">
          <el-select
            v-model="police"
            clearable
            placeholder="请选择部门"
            style="width: 350px"
          >
            <el-option
              v-for="item in department"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="管理人员">
          <!-- <el-input v-model="form.column" style="width: 350px"></el-input> -->
          <el-select
            v-model="police"
            clearable
            placeholder="请选择部门"
            style="width: 350px"
          >
            <el-option
              v-for="item in department"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备状态">
          <el-radio-group v-model="form.resource">
            <el-radio :label="1">运行</el-radio>
            <el-radio :label="2">停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="plassets = false">取 消</el-button>
          <el-button type="primary" v-if="under == 1" @click="determine()"
            >确 定</el-button
          >
          <el-button @click="hardware()" v-else type="primary"
            >编辑 确 定</el-button
          >
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
// import { defineComponent } from "vue";
import { ElMessage } from "element-plus";
import dayjs from "dayjs";

export default {
  data() {
    return {
      value2: "",
      plassets: false,
      foodName: "",
      value1: "",
      form: {
        number: "",
        offering: "",
        mock: "",
        dates: "",
        column: "",
        resource: "",
      },
      underID: "",
      department: [
        {
          value: "0",
          label: "销售部门",
        },
        {
          value: "1",
          label: "IT部门",
        },
      ],
      police: "", //使用部门
      tableData: [],
      under: 1,
      single: "",
      headerObj: "", //请求头
      m_page: {
        sizes: [10, 20, 40, 50, 100], //每页最大显示数
        size: 10,
        totalElements: 0,
        totalPages: 3,
        number: 1,
      },
    };
  },
  created() {
    let str = sessionStorage.getItem("token");
    // this.headerObj["Authorization"] = `Bearer ${str}`;
    this.headerObj = `Bearer ${str}`;
  },
  beforeMount() {
    this.production(); //获取固定资产
  },
  methods: {
    searchType() {},
    //编辑确定
    hardware() {
      this.$axios
        .post(
          `api/serial/FixedAssets`,
          {
            id: this.underID,
            equipmentNumber: this.form.number,
            equipmentName: this.form.offering,
            model: this.form.mock,
            startDate: this.form.dates,
            userDepartment: this.police,
            UserDepartmentId: 1,
            Management: this.form.column,
            managementId: 1,
            deviceStatus: this.form.resource,
          },
          {
            headers: {
              Authorization: this.headerObj,
            },
          }
        )
        .then(() => {
          ElMessage({
            showClose: true,
            message: "编辑成功",
            type: "success",
          });
          this.plassets = false;
          this.production();
        })
        .catch(() => {
          this.$message.error("编辑失败");
        });
    },
    //编辑
    editorPara(row, index) {
      this.under = index;
      this.single = "编辑资产";
      this.plassets = true;
      let authors = row;
      // console.log(authors);
      this.underID = authors.id;
      this.form.number = authors.equipmentNumber;
      this.form.offering = authors.equipmentName;
      this.form.mock = authors.model;
      this.form.dates = authors.startDate;
      this.police = authors.userDepartment;
      this.form.column = authors.managementId;
      this.form.resource = authors.deviceStatus;
    },
    //删除
    deleted(row) {
      console.log(row);
      let into = row;
      this.$confirm("确认删除该设备?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios
            .post(
              `api/serial/FixedAssets`,
              {
                id: into.id,
                equipmentNumber: into.equipmentNumber,
                equipmentName: into.equipmentName,
                model: into.model,
                startDate: into.startDate,
                userDepartment: into.userDepartment,
                UserDepartmentId: 1,
                Management: into.managementId,
                managementId: 1,
                deviceStatus: into.deviceStatus,
                isDelete: 1,
              },
              {
                headers: {
                  Authorization: this.headerObj,
                },
              }
            )
            .then(() => {
              // console.log(res);
              this.production();
              this.$message.success("删除成功");
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //新增弹出
    addition(index) {
      // console.log(index);
      this.under = index;
      this.single = "新增资产";
      this.plassets = true;
      this.form.number = "";
      this.form.offering = "";
      this.form.mock = "";
      this.form.dates = "";
      this.police = "";
      this.form.column = "";
      this.form.resource = "";
    },
    //保存
    determine() {
      // console.log(this.value2);
      this.$axios
        .post(
          `api/serial/FixedAssets`,

          {
            id: 0,
            equipmentNumber: this.form.number, //设备编号
            equipmentName: this.form.offering, //设备名称
            model: this.form.mock,
            startDate: this.form.dates,
            userDepartment: this.police,
            UserDepartmentId: 1,
            Management: this.form.column,
            managementId: 1,
            deviceStatus: this.form.resource,
          },
          {
            headers: {
              Authorization: this.headerObj,
            },
          }
        )
        .then(() => {
          // console.log(res);
          // if(res.code==200)
          ElMessage({
            showClose: true,
            message: "保存成功",
            type: "success",
          });
          this.plassets = false;
          this.production();
        })
        .catch(() => {
          this.$message.error("保存失败");
        });
    },
    notEmpty() {
      this.foodName = "";
      this.production();
    },
    //获取
    production() {
      // this.$axios
      //   .get(
      //     `api/serial/FixedAssets?pageSize=${this.m_page.size}&pageIndex=${this.m_page.number}&query={equipmentNumber=${this.foodName}}`,
      //     {
      //       headers: {
      //         Authorization: this.headerObj,
      //       },
      //     }
      //   )
      //   .then((res) => {
      //     console.log(res);
      //     this.tableData = res.data.data;
      //     this.m_page.totalElements = res.data.count;
      //   });

      this.$axios({
        methods: "GET",
        url: "api/serial/FixedAssets",
        headers: {
          Authorization: this.headerObj,
        },
        params: {
          pageSize: this.m_page.size,
          pageIndex: this.m_page.number,
          query: {
            // equipmentNumber: this.foodName,
            equipmentName: this.foodName,
            // model: this.foodName,
            // managementId: this.foodName,
          },
        },
      }).then((res) => {
        ({ data: this.tableData, count: this.m_page.totalElements } = res.data);
        this.tableData.forEach((item) => {
          item.startDate = dayjs(item.startDate).format("YYYY-M-D");
        });
      });
    },
    //页码
    m_handlePageChange(currPage) {
      this.m_page.number = currPage;
      this.production();
    },
    m_handleSizeChange(currSize) {
      this.m_page.size = currSize;
      this.production();
    },
    //时间转换
    dateFormat(dateData) {
      var date = new Date(dateData);
      var y = date.getFullYear();
      var m = date.getMonth() + 1;
      m = m < 10 ? "0" + m : m;
      var d = date.getDate();
      d = d < 10 ? "0" + d : d;
      const time = y + "-" + m + "-" + d;
      return time;
    },
  },
};
</script>

<style scoped>
.pagingClass {
  text-align: right;
  height: 40px;
  line-height: 40px;
  /* margin: 20px 0; */
  background-color: #fff;
  margin-bottom: 40px;
}
</style>